<template>
  <!-- sync修饰符只能修改当前组件的data数据，这是父组件传过来的数据 -->
  <el-dialog :visible="showDialog" title="新增子部门" @close="btnCancel">
    <!-- 内容 -->
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="120px">
      <el-form-item label="线路编号：" prop="number">
        <el-input v-model="ruleForm.number" placeholder="请输入线路名称" class="input80" />
      </el-form-item>
      <el-form-item label="线路名称：" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请输入线路名称" class="input80" />
      </el-form-item>
      <el-form-item label="线路类型：" prop="type">
        <el-select v-model="ruleForm.type" placeholder="请选择线路类型" style="width:80%">
          <el-option value="干线" />
          <el-option value="支线" />
          <el-option value="接驳路线" />
        </el-select>
      </el-form-item>
      <el-form-item label="起始地机构：" prop="startOrganName">
        <el-select v-model="ruleForm.startOrganName" placeholder="请选择起始地机构" style="width:80%">
          <el-option v-for="item in organizationList" :key="item.type" :label="item.name" :value="item.type" />
        </el-select>
      </el-form-item>
      <el-form-item label="目的机构：" prop="endOrganName">
        <el-select v-model="ruleForm.endOrganName" placeholder="请选择目的机构" style="width:80%">
          <el-option v-for="item in organizationList" :key="item.type" :label="item.name" :value="item.type" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="btnOK">确认</el-button>
        <el-button @click="btnCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { transportTree } from '@/api/Transport-interface'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    var checkType = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('线路类型不能为空'))
      } else {
        callback()
      }
    }
    var validateNumber = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('线路编号不能为空'))
      } else {
        const reg = /[A-Z]{2}\d{6}/
        if (!reg.test(value)) {
          callback(new Error('线路编号前两位为XL,后6位数字,请重新输入'))
        } else {
          callback()
        }
      }
    }
    return {
      ruleForm: {
        number: '',
        name: '',
        type: '',
        startOrganName: '',
        endOrganName: ''
      },
      rules: {
        number: [
          { required: true, validator: validateNumber, trigger: 'blur' }
        ],
        name: [
          { required: true, message: '线路名称不能为空', trigger: 'blur' }
        ],
        type: [
          { validator: checkType, trigger: 'blur' }
        ],
        startOrganName: [{ required: true, message: '起始地机构不能为空', trigger: 'blur' }],
        endOrganName: [{ required: true, message: '目的地机构不能为空', trigger: 'blur' }]
      },
      AddLine: [],
      organizationList: []
    }
  },
  // 获取数据
  async created() {
    const res = await transportTree()
    const data = JSON.parse(res)
    this.organizationList = data
    console.log(data)
  },
  methods: {
    btnCancel() {
      this.$emit('update:showDialog', false)
    },

    async transportTree() {
      // this.AddLine = await transportTree()
    },
    async btnOK() {
      await this.$refs.ruleForm.validate()
      // 调用接口
      await transportTree(this.ruleForm)
      // 提示消息
      this.$message.success('添加线路成功')
      this.btnCancel()
    }
    // btnCancel() {
    //   this.$refs.ruleForm.resetFields() // 重置表单清除校验
    //   this.showDialog = false
    // }
  }

}
</script>

<style>

</style>
